<template>
  <view style="height: 800rpx">
    <!-- pages/smartlock_test/smartlock_test.wxml -->
    <!-- <view>常用宿舍为：{{ usual }}</view> -->
    <van-circle :value="value" size="300" stroke-width="6" speed="100" :text='text' />
    <view style="display: none" class="connected_info" v-if="connected">
      <view>
        <text>已连接到 {{ name }}</text>
        <view class="operation">
          <button size="mini" @tap="closeBLEConnection">断开连接</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import vanCircle from '../../wxcomponents/vant/circle/index.js'
  var apartmentData;
  export default {
    components: {
      vanCircle
    },
    data() {
      return {
        value: 0,
        text: '正在连接',

        // 剩下的进度条,为了使环形进度条实现动画闭合效果
        num: 100,

        // 常用宿舍号
        // usual: '',

        // 用于存放蓝牙设备的数组
        devices: [],

        // 是否连接成功，决定了开门的成功与失败
        connected: false,

        chs: [],

        // 没有扫描
        misScanding: false,

        scandbutName: '',
        name: '',
        deviceId: '',
        canWrite: false
      };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
      // this.openBluetoothAdapter();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
      this.value = this.value + this.num
      if (this.connected === true) {
        // 延迟显示提示信息
        setTimeout(() => {
          this.text = '开门成功'
        }, 1500);
      } else {
        // 延迟显示提示信息
        setTimeout(() => {
          this.text = '开门失败'
        }, 1500);
        // 显示开门失败后返回首页
        setTimeout(
          () =>
          uni.navigateBack({
            delta: 1
          }),
          2000
        );
      }
    },
    onShow() {
      // this.getDataStroage();
      // // 重新设置usual的值显示到页面上
      // this.setData({
      //     usual: apartmentData[0].text
      // });
    },
    methods: {
      // 从本地缓存中读取常用宿舍数据
      // getDataStroage() {
      //     // 同步读取本地缓存数据
      //     apartmentData = uni.getStorageSync('apartment');
      //     console.log(apartmentData);
      // },

      // 初始化蓝牙模块
      openBluetoothAdapter() {
        this.misScanding = false;
        uni.openBluetoothAdapter({
          success: (res) => {
            console.log('openBluetoothAdapter success', res);
            this.startBluetoothDevicesDiscovery();
          },
          fail: (res) => {
            if (res.errCode === 10001) {
              uni.onBluetoothAdapterStateChange(function(res) {
                console.log('onBluetoothAdapterStateChange', res);
                if (res.available) {
                  this.startBluetoothDevicesDiscovery();
                }
              });
            }
          }
        });
      },

      // 获取蓝牙适配器状态码
      getBluetoothAdapterState() {
        uni.getBluetoothAdapterState({
          success: (res) => {
            console.log('getBluetoothAdapterState', res);
            if (res.discovering) {
              this.onBluetoothDeviceFound();
            } else if (res.available) {
              this.startBluetoothDevicesDiscovery();
            }
          }
        });
      },

      // 开始搜寻附近的蓝牙外围设备
      startBluetoothDevicesDiscovery() {
        var that = this;
        if (this._discoveryStarted) {
          this.stopBluetoothDevicesDiscovery();
          return;
        }
        this.setData({
          misScanding: true,
          scandbutName: '正在搜索，点击停止',
          devices: [],
          chs: []
        });
        //console.log('lisn3188---------- misScanding = ', this.misScanding)
        this._discoveryStarted = true;
        uni.startBluetoothDevicesDiscovery({
          allowDuplicatesKey: true,
          success: (res) => {
            setTimeout(function() {
              console.log('----BluetoothDevicesDiscovery finish---- ');
              if (that._discoveryStarted) {
                that.stopBluetoothDevicesDiscovery();
              }
            }, 20000);
            console.log('startBluetoothDevicesDiscovery success', res);
            this.onBluetoothDeviceFound();
          }
        });
      },

      // 找到要搜索的设备后，及时停止扫描
      stopBluetoothDevicesDiscovery() {
        this._discoveryStarted = false;
        uni.stopBluetoothDevicesDiscovery();
        this.setData({
          misScanding: false,
          scandbutName: '重新刷新列表'
        });
        //console.log('lisn3188---------- misScanding = ', this.misScanding)
      },

      // 监听扫描到新设备事件
      onBluetoothDeviceFound() {
        uni.onBluetoothDeviceFound((res) => {
          res.devices.forEach((device) => {
            if (!device.name && !device.localName) {
              return;
            }
            const foundDevices = this.devices;
            const idx = inArray(foundDevices, 'deviceId', device.deviceId);
            const data = {};
            if (idx === -1) {
              data[`devices[${foundDevices.length}]`] = device;
            } else {
              data[`devices[${idx}]`] = device;
            }
            this.setData(data);
          });
        });
      },

      goto_Comm(e) {
        app.globalData.ble_device = e.currentTarget.dataset;
        this.stopBluetoothDevicesDiscovery();
        uni.navigateTo({
          url: '/pages/comm/comm'
        });
      },

      // 蓝牙低功耗设备间要进行通信，必须首先建立连接
      createBLEConnection(e) {
        const ds = e.currentTarget.dataset;
        const deviceId = ds.deviceId;
        const name = ds.name;
        uni.createBLEConnection({
          deviceId,
          success: (res) => {
            this.setData({
              connected: true,
              name,
              deviceId
            });
            this.getBLEDeviceServices(deviceId);
          }
        });
        this.stopBluetoothDevicesDiscovery();
      },

      // 断开蓝牙连接
      closeBLEConnection() {
        uni.closeBLEConnection({
          deviceId: this.deviceId
        });
        this.setData({
          connected: false,
          chs: [],
          canWrite: false
        });
      },

      // 获取蓝牙外围设备的服务
      getBLEDeviceServices(deviceId) {
        uni.getBLEDeviceServices({
          deviceId,
          success: (res) => {
            for (let i = 0; i < res.services.length; i++) {
              if (res.services[i].isPrimary) {
                this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
                return;
              }
            }
          }
        });
      },

      // 获取蓝牙外围设备的服务特征值
      getBLEDeviceCharacteristics(deviceId, serviceId) {
        uni.getBLEDeviceCharacteristics({
          deviceId,
          serviceId,
          success: (res) => {
            console.log('getBLEDeviceCharacteristics success', res.characteristics);
            for (let i = 0; i < res.characteristics.length; i++) {
              let item = res.characteristics[i];
              if (item.properties.read) {
                uni.readBLECharacteristicValue({
                  deviceId,
                  serviceId,
                  characteristicId: item.uuid
                });
              }
              if (item.properties.write) {
                this.setData({
                  canWrite: true
                });
                this._deviceId = deviceId;
                this._serviceId = serviceId;
                this._characteristicId = item.uuid;
                this.writeBLECharacteristicValue();
              }
              if (item.properties.notify || item.properties.indicate) {
                uni.notifyBLECharacteristicValueChange({
                  deviceId,
                  serviceId,
                  characteristicId: item.uuid,
                  state: true
                });
              }
            }
          },
          fail(res) {
            console.error('getBLEDeviceCharacteristics', res);
          }
        });
        // 操作之前先监听，保证第一时间获取数据
        uni.onBLECharacteristicValueChange((characteristic) => {
          const idx = inArray(this.chs, 'uuid', characteristic.characteristicId);
          const data = {};
          if (idx === -1) {
            data[`chs[${this.chs.length}]`] = {
              uuid: characteristic.characteristicId,
              value: ab2hex(characteristic.value)
            };
          } else {
            data[`chs[${idx}]`] = {
              uuid: characteristic.characteristicId,
              value: ab2hex(characteristic.value)
            };
          }
          // data[`chs[${this.data.chs.length}]`] = {
          //   uuid: characteristic.characteristicId,
          //   value: ab2hex(characteristic.value)
          // }
          this.setData(data);
        });
      },

      // 写入蓝牙服务特征值
      writeBLECharacteristicValue() {
        // 向蓝牙设备发送一个0x00的16进制数据
        let buffer = new ArrayBuffer(1);
        let dataView = new DataView(buffer);
        dataView.setUint8(0, (Math.random() * 255) | 0);
        uni.writeBLECharacteristicValue({
          deviceId: this._deviceId,
          serviceId: this._deviceId,
          characteristicId: this._characteristicId,
          value: buffer
        });
      },

      // 关闭蓝牙适配器
      closeBluetoothAdapter() {
        uni.closeBluetoothAdapter();
        this._discoveryStarted = false;
      },

      // 前往设置uuid页面
      gotosetuuid() {
        uni.navigateTo({
          url: '/pages/setuuid/setuuid'
        });
      }
    }
  };
</script>
<style>
  /* pages/smartlock_test/smartlock_test.wxss */
  body {
    color: #333;
    height: 844rpx;
  }

  .devices_summary {
    padding: 10px;
    font-size: 16px;
    background-color: hsl(216, 5%, 22%);
    color: #fff;
    align-items: center;
    justify-content: center;
    display: flex;
  }

  .device_list {
    height: auto;
    margin: 50px 5px;
    margin-top: 5px;
    border: 1px solid #eee;
    border-radius: 5px;
    width: auto;
  }

  .device_item {
    border-bottom: 1px solid #222;
    padding: 10px;
    color: #666;
  }

  .device_item_hover {
    background-color: rgba(0, 0, 0, 0.1);
  }

  .connected_info {
    position: fixed;
    bottom: 0;
    width: 100%;
    background-color: #f0f0f0;
    padding: 10px;
    padding-bottom: 20px;
    margin-bottom: env(safe-area-inset-bottom);
    font-size: 14px;
    min-height: 100px;
    box-shadow: 0px 0px 3px 0px;
  }

  .connected_info .operation {
    position: absolute;
    display: inline-block;
    right: 30px;
  }

  .button_sp {
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 5px;
    background-color: #10b7ff;
    color: #fff;
    font-size: 15px;
  }

  .button_sp2 {
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 5px;
    background-color: #757575;
    color: #fff;
  }

  .flex-wrp {
    height: auto;
    display: flex;
    background-color: #ffffff;
  }

  /*横向布局  */
  .layout_horizontal {
    display: flex;
    /*row 横向  column 列表  */
    flex-direction: row;
  }

  /*纵向布局  */
  .layout_vertical {
    height: 100rpx;
    display: flex;
    /*row 横向  column 列表  */
    flex-direction: column;
  }

  /*划线  */
  .line {
    background-color: blue;
    height: 5px;
    width: 100%;
  }

  .div {
    background-color: white;
    height: 10px;
    width: 10px;
  }

  van-circle {
    display: flex;
    /*row 横向  column 列表  */
    height: 100%;
    justify-content: center;
    align-items: center;
  }
</style>